﻿using System;
using System.Collections.Generic;
using System.Linq;
using LBoL.Base;
using LBoL.ConfigData;
using LBoL.Core;
using LBoL.Core.Battle;
using LBoL.Core.Battle.BattleActions;
using LBoL.Core.Battle.Interactions;
using LBoL.Core.Cards;
using LBoLEntitySideloader.Attributes;
using ProbeMod.Cards.Template;

namespace ProbeMod.Cards
{
    public sealed class ProbeWhirlwindDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            var config = GetCardDefaultConfig();

            config.Rarity = Rarity.Common;
            config.Type = CardType.Attack;
            config.TargetType = TargetType.SingleEnemy;
            config.Colors = new List<ManaColor>() { ManaColor.Blue };

            config.Cost = new ManaGroup() { Blue = 1 };
            config.UpgradedCost = new ManaGroup() { Blue = 1 };

            config.Value1 = 1;
            config.Value2 = 2;
            

            config.Damage = 12;
            config.UpgradedDamage = 15;

            config.Index = CardIndexGenerator.GetUniqueIndex(config);
            return config;
        }
    }

    /// <summary>
    /// 旋风斩
    /// 打7，最多弃1/2牌，再对随机弃牌数量目标造成7点伤害。
    /// </summary>
    [EntityLogic(typeof(ProbeWhirlwindDef))]
    public sealed class ProbeWhirlwind : ProbeCard
    {
        public int DiscardCountMax { get => IsUpgraded?3:2; }

        //private List<Card> _allHand;

        public override bool DiscardCard => true;

        protected override IEnumerable<BattleAction> Actions(UnitSelector selector, ManaGroup consumingMana, Interaction precondition)
        {
            yield return AttackAction(selector);
            if (base.Battle.BattleShouldEnd)
            {
                yield break;
            }
            List<Card> list = base.Battle.HandZone.ToList();
            if (list.Count > 0)
            {
                SelectHandInteraction interaction = new SelectHandInteraction(0, DiscardCountMax, list)
                {
                    Source = this
                };
                yield return new InteractionAction(interaction);
                IReadOnlyList<Card> cards = interaction.SelectedCards;
                yield return new DiscardManyAction(cards);
                if (cards.Count > 0)
                {
                    for (int i = 0;  i < cards.Count;  i++)
                    {
                        var enemy = Battle.RandomAliveEnemy;
                        yield return AttackAction(enemy,Damage,"Simple1");
                    }
                }
            }
        }
    }
}
